﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TaskManagement.ModelLayer;

namespace DataAccess
{
    public class Converter
    {
        /// <summary>
        /// Converts a Task to a TaskEntity
        /// </summary>
        /// <param name="task">The Task to convert</param>
        /// <returns>The TaskEntity</returns>
        public static TaskEntity ConvertTaskEntity(Task task)
        {
            TaskEntity te = new TaskEntity();
            te.Id = task.Id;
            te.Account = task.Account;
            te.Date = (DateTime)task.Date;
            te.Delivery = task.Delivery;
            te.Description = task.Description;
            te.EmployeeEntity = ConvertEmployeEntity(task.Employee);
            te.Estimate = task.Estiamte;
            te.TimeSpent = task.TimeSpent;
            te.Status = task.Status;
            te.Tag = task.Tag;
            te.WorkflowEntity = ConvertWorkflowEntity(task.workflow);

            return te;
        }


        /// <summary>
        /// Converts a TaskEntity into a task
        /// </summary>
        /// <param name="te">The taskEntity</param>
        /// <returns>The task representation of te</returns>
        public static Task ConvertTask(TaskEntity te)
        {
            Task t = new Task();
            t.Id = te.Id;
            t.Status = te.Status;
            t.Tag = te.Tag;
            t.TimeSpent = te.TimeSpent;
            t.Account = te.Account;
            t.Date = te.Date;
            t.Delivery = te.Delivery;
            t.Estiamte = te.Estimate;
            t.Description = te.Description;
            t.Predecessors = ConvertTasktList(te.Predecessors);
            t.Successors = ConvertTasktList(te.Successors);
            t.Employee = ConvertEmployee(te.EmployeeEntity);
            t.workflow = ConvertWorkflow(te.WorkflowEntity);

            return t;
        }

        public static List<Task> ConvertTasktList(ICollection<TaskEntity> taskEntitys)
        {
            List<Task> results = new List<Task>();

            foreach (TaskEntity te in taskEntitys)
            {
                results.Add(ConvertTask(te));
            }

            return results;
        }

        public static Employee ConvertEmployee(EmployeeEntity ee)
        {
            Employee employee = null;

            if (ee != null)
            {
                employee = new Employee();
                employee.Email = ee.Email;
                employee.FirstName = ee.FirstName;
                employee.Id = ee.Id;
                employee.LastName = ee.LastName;
                employee.PhoneNumber = ee.PhoneNumber;
                employee.EmployedAs = ee.EmployedAs;
                //employee.Tasks = ConvertTasktList(ee.TaskEntitys);
            }
            return employee;
        }

        public static EmployeeEntity ConvertEmployeEntity(Employee e)
        {
            EmployeeEntity eEntity = null;
            if(e != null)
            {
            eEntity = new EmployeeEntity();
            eEntity.Email = e.Email;
            eEntity.EmployedAs = e.EmployedAs;
            eEntity.FirstName = e.FirstName;
            eEntity.LastName = e.LastName;
            eEntity.PhoneNumber = e.PhoneNumber;
            } 
            
            return eEntity;
        }

        public static WorkflowEntity ConvertWorkflowEntity(Workflow w)
        {
            WorkflowEntity wEntity = null;
            if(w != null)
            {
                wEntity = new WorkflowEntity();
                wEntity.Id = w.Id;
                wEntity.Description = w.Description;
                wEntity.Name = w.Name;
                wEntity.StartDate = w.StartDate;
            }

            return wEntity;
        }

        public static Workflow ConvertWorkflow(WorkflowEntity we)
        {
            Workflow w = null;
            if (we!= null)
            {
                w = new Workflow();
                w.Id = we.Id;
                w.Name = we.Name;
                w.StartDate = we.StartDate;
                w.Description = we.Description;
            }

            return w;
        }
    }
}
